package com.cet.designapplication.deprecated.state;

import com.cet.designapplication.utils.RedisCommonProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author NSOC
 */

@Component
public class DeprecatedWaitReceive extends DeprecatedAbstractOrderState {

    @Autowired
    private RedisCommonProcessor redisCommonProcessor;

    @Override
    protected DeprecatedOrder receiveOrder(String orderId, DeprecateOrderContext context) {
        DeprecatedOrder order = (DeprecatedOrder) redisCommonProcessor.get(orderId);
        if (!order.getState().equals(ORDER_WAIT_RECEIVE)) {
            throw new UnsupportedOperationException("Order state expect to be ORDER_WAIT_RECEIVE, now is " + order.getState());
        }

        order.setState(ORDER_FINISHED);
        redisCommonProcessor.remove(orderId);
        notifyObservers(orderId, ORDER_FINISHED);
        return order;
    }
}
